using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
namespace HHHTool {
    public class ReadINI
    {
        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        private static extern bool WritePrivateProfileString(
            string lpAppName, string lpKeyName, string lpString, string lpFileName);
        [DllImport("kernel32", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int GetPrivateProfileString(
            string lpAppName, string lpKeyName, string lpDefault, StringBuilder lpReturnedString,
            int nSize, string lpFileName);



        public string ReadConfigIni(string configType, string configName, string filePath)
        {
            StringBuilder temp = new StringBuilder(16);
            GetPrivateProfileString(configType, configName, "", temp, 16, filePath);
            if (temp.ToString() == "") { Debug.LogError("未能读取相应信息"); }
            Debug.Log($"读取到信息：{configType}-{configName}-{temp.ToString()}");
            return temp.ToString();
        }

        public void WriteConfigIni(string configType, string configName, string info, string filePath)
        {
            if (WritePrivateProfileString(configType, configName, info, filePath))
            {
                Debug.Log("写入成功");

            }
            else
            {
                Debug.Log("写入失败");
            }
        }
        public string ReadIPIni(string socketName, string filePath)
        {
            StringBuilder temp = new StringBuilder(16);
            GetPrivateProfileString("SocketConfig", socketName, "1,1,1,1", temp, 16, filePath);
            if (temp.ToString() == "1,1,1,1") { Debug.LogError("IP地址获取错误"); }
            Debug.Log("deviceName" + temp.ToString());
            return temp.ToString();
        }
        public int ReadResolutionIni(string resolutionParamater, string filePath)
        {
            StringBuilder temp = new StringBuilder(16);
            GetPrivateProfileString("SocketConfig", resolutionParamater, "1920", temp, 16, filePath);
            if (int.Parse(temp.ToString()) == default) { Debug.LogError("分辨率有问题"); }
            Debug.Log("resolutionParamater" + temp.ToString());
            return int.Parse(temp.ToString());
        }
    }
}

